// pages/malls/malls-index/mall-index.js
const app = getApp()
const constant = require("../../../constant.js")
const storage = require("../../../utils/storage.js")
const auth = require("../../../utils/auth.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    onlineShow: false,
    offlineShow: false,
    needAuthBtn: true,
    qiniu: constant.resource.qiniu,
    offlinevideoSrc: constant.resource.qiniu + "/weapp/1v1.mp4",
    onlineVideoSrc: constant.resource.qiniu + "/weapp/camp.mp4"
  },

  bindClickOffline: function() {
    this.videoOfflineContext.play()
    this.videoOfflineContext.seek(0)
    this.setData({
      onlineShow: false,
      offlineShow: !this.data.offlineShow,
    })
  },
  
  bindClickOfflineVideo: function() {
    this.videoOfflineContext.pause()
    this.setData({
      offlineShow: !this.data.offlineShow
    })
    
  },

  bindClickOnline: function () {
    this.setData({
      offlineShow: false,
      onlineShow: !this.data.onlineShow
    })
    this.videoOnlineContext.play()
    this.videoOnlineContext.seek(0)
  },

  stopOnlineVideo: function() {
    this.videoOnlineContext.pause()
    console.log(this.data.offlineShow)
    this.setData({
  
      onlineShow: !this.data.onlineShow
    })
  },

  // 绑定授权逻辑，授权成功后的跳转逻辑
  bindGetUserInfo: function (e) {
    console.log(e.detail)
    var that = this;
    var app = getApp();
    try {
      auth._handleGetUserInfoSuccess(e.detail.iv, e.detail.encryptedData, app.globalData.authKey, function() {
        debugger
        wx.redirectTo({
          url: '/pages/profile/profile',
        })
      }, function() {
        debugger
        wx.showToast({
          title: '授权失败',
        })
      }, false) 
    } catch (e) {
      console.log(e)
      console.log("no unionid in storgae start auth")
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.setData({
     qiniu: constant.resource.qiniu,
     mobile: wx.getStorageSync(storage.keys.mobile)
   })
   
   // 校验按钮是否必须需要授权
   auth.checkAuthBtn(this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.videoOfflineContext = wx.createVideoContext('offlineVideo')
    this.videoOnlineContext = wx.createVideoContext('onlineVideo')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
    this.setData({
      onlineShow: true,
      offlineShow: true,
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})